let app = getApp()
Component({
    properties: {
      good_type:Number,
      productlist:Array,
      type_id:Number,
      orderid:String,
      activity_id:String,
      page_type:String,
      message: {
        type:Array,
        observer: function(newVal, oldVal) {
          this.updatemessage()
           }
        }
      },
      data:{ 
        waitting:false,
        message_index:0,
        goods_type:0,
        isChooseAddress:false,
        noSubmitOrder:true,
        coupon_price:0,
        deliver_cost:0,
        integral_score:0,
        coupon_id:0,
        deliver_type:1,
        pay_type:0,
        order_type:1,
        order_goods_type:1,
        moneySign:'￥',
        activeDeliverMethod:-1,
        activeCouponMethod:-1,
        array: [],
        index:0,
        deliver_type:1,
        buyer_remarks:'',
        coupon_list:[],
        error_show:false,
        showAddress: {
          id: 0,
          title: "查看配送范围",
          right: "去查看",
          link: {
              type: "show_map",
              longitude: "",
              latitude: "",
              covers: "",
              circles: "",
              scale: 13,
              title: "同城配送范围"
          }
      },
        chooseAddress: {
          id: 0,
          title: "暂无收货地址",
          right: "去添加",
          link: { 
              type: "address",
              parent: "confirm_order_address"
          }
      }, 
      },
      pageLifetimes: {
        show: function () {
           this.setData({
            globalColor:app.globalData.globalColor
          })
        this.userAddressList();
         var selfAddress=[ {
          title: "选择自提点", 
          right:wx.getStorageSync('selfAddr')!=''?wx.getStorageSync('selfAddr'):'请选择自提点',
          link: {
              type: "selfaddress",
              parent: "confirm_order_selfmention",
              list: JSON.stringify(this.data.selfmention_list)
          }
      }, {
          title: "提货人姓名",
          rightType: "input",
          name: "receiverName",
          hiddenArrow: !0,
          input: {
              value:wx.getStorageSync('receiverName'),
              placeholder: "输入提货人姓名"
          }
      }, {
          title: "提货人手机号",
          rightType: "input",
          name: "receiverPhone",
          hiddenArrow: !0,
          input: {
              value:wx.getStorageSync('receiverPhone'),
              placeholder: "输入提货人手机号"
          }
      } ]
      this.setData({
        selfAddress:selfAddress
      })
      },
      },
    
    lifetimes: {
        attached: function () {
            this.userAddressList();
            this.page_data();
            this.setData({
              getGlobalColor:app.globalData.globalColor
            })
            if(typeof(this.data.good_type)!='undefined'&&this.data.good_type==3){
                var goods_type=this.data.good_type
                this.setData({
                  activeDeliverMethod:6,
                  goods_type:goods_type
                })
                this.Order_orderPrice2(this.data.good_type);
            }
            if(typeof(this.data.good_type)!='undefined'&&this.data.good_type==4){
              var goods_type=this.data.good_type
              this.setData({
                activeDeliverMethod:6,
                goods_type:goods_type
              })
              this.Order_orderPrice2(this.data.good_type);
          }
       }
      },
    methods: {
      Order_orderPrice2:function(good_type){
        var that=this
        wx.request({
          url: app.globalData.siteroot,
           data: {
             do:'Order_orderPrice',
             address_id:that.data.address_id,
             deliver_type:1,
             pay_type: 0,
             activity_id:that.data.activity_id,
             launch_id:that.data.orderid,
             user_id:wx.getStorageSync('user_id'),
             order_type:1,
             order_goods_type:good_type,
             goods:wx.getStorageSync('confirmorder')
          },
         success: function(res) {
           var allCount=0;
            for (var i = 0; i <res.data.data.goods_list.length; i++) {
              allCount=parseInt(allCount)+parseInt(res.data.data.goods_list[i]['num'])
              res.data.data.goods_list[i]['price']=res.data.data.goods_prices
              res.data.data.goods_list[i]['count']=res.data.data.goods_list[i]['num']
              res.data.data.goods_list[i]['hideCount']=true
            }
            if(res.data.data.deliver_type.length>0){
              that.setData({
               activeDeliverMethod:res.data.data.deliver_type[0].id
              })
            }
            that.setData({
              selfmention_list:res.data.data.selfmention_list,
              order_type:1,
              order_goods_type:good_type,
              allCount:allCount,
              array:res.data.data.deliver_type,
              productData:res.data.data.goods_list,
              totalprice:res.data.data.total_price,
              goods_prices:res.data.data.goods_prices,
              cut_money:res.data.data.cut_money,
              deliver_cost:res.data.data.deliver_cost
            })
            that.check_message();
         }
       })
      },
      userAddressList:function(){
        var that=this
        wx.request({
          url: app.globalData.siteroot,
           data: {
             do:'Member_userAddressList',
             user_id:wx.getStorageSync('user_id')
          },
         success: function(res) {
              for (var i = 0; i <res.data.data.length; i++) {
                if(res.data.data[i]['is_default']==1)
                {
                  var province=res.data.data[i].province!=null?res.data.data[i].province:'';
                  var city=res.data.data[i].city!=null?res.data.data[i].city:'';
                  var district=res.data.data[i].district!=null?res.data.data[i].district:'';
                  var address={
                    id:res.data.data[i].id,
                    title:res.data.data[i].consigner+'  '+res.data.data[i].phone, 
                    desc:province+' '+city+' '+district+' '+res.data.data[i].address,
                    icon: "../../static/icon/icon_address_36.png",
                    link: {
                        type: "address",
                        parent: "confirm_order_address"
                    }}
                  res.data.data[i]['icon']='../../static/icon/icon_address_36.png'
                  res.data.data[i]['title']=res.data.data[i]['consigner']+' '+res.data.data[i]['phone']
                  res.data.data[i]['desc']=res.data.data[i]['city']+' '+res.data.data[i]['district']+' '+res.data.data[i]['address']
                  that.setData({
                    chooseAddress:address,
                    address_id:res.data.data[i].id
                  })
                }
              }
              that.page_data();
         }
       })
      },
      page_data:function(){
        var that=this
        if(that.data.page_type=='bargain'){
          that.Order_orderPrice();
        }else{
          if(that.data.type_id==0)
          {
            var productData=wx.getStorageSync('confirmorder')
            for (var i = 0; i <productData.length; i++) {
              productData[i]['num']=productData[i]['count']
            }
            that.setData({ 
              productData:productData
            })
            this.check_message();
            
            if(that.data.page_type!='seckill'&&that.data.page_type!='series'){
              that.updatedata();
            }else{
              if(that.data.page_type=='seckill'){
                console.log('confirmorder')
                console.log(wx.getStorageSync('confirmorder'))
                that.setData({
                  order_type:3,
                  activity:'seckill',
                  activity_id:wx.getStorageSync('confirmorder')[0].activity_id
                })
              }
              that.totalprice();
              var goods_prices=0;
              for (var i = 0; i <productData.length; i++) {
                goods_prices=goods_prices+productData[i]['num']*productData[i]['price'];
              }
              that.setData({
                goods_prices:goods_prices
              })
            }
          
          }
          else{
            that.updatedata();
          }
        }
    
      },
      Order_orderPrice:function(){
        var that=this
        if(this.data.page_type=='bargain'){
          wx.request({
            url: app.globalData.siteroot,
             data: {
               do:'Order_orderPrice',
               address_id:that.data.address_id,
               deliver_type:1,
               pay_type: 0,
               activity_id:that.data.activity_id,
               launch_id:that.data.orderid,
               user_id:wx.getStorageSync('user_id'),
               order_type:2,
               order_goods_type:'',
               goods:wx.getStorageSync('confirmgoods')
            },
           success: function(res) {
             var allCount=0;
              for (var i = 0; i <res.data.data.goods_list.length; i++) {
                allCount=parseInt(allCount)+parseInt(res.data.data.goods_list[i]['num'])
                res.data.data.goods_list[i]['price']=res.data.data.goods_prices
                res.data.data.goods_list[i]['count']=res.data.data.goods_list[i]['num']
                res.data.data.goods_list[i]['hideCount']=true
              }
              if(res.data.data.deliver_type.length>0){
                that.setData({
                 activeDeliverMethod:res.data.data.deliver_type[0].id
                })
              }
              that.setData({
                selfmention_list:res.data.data.selfmention_list,
                order_type:2,
                allCount:allCount,
                array:res.data.data.deliver_type,
                productData:res.data.data.goods_list,
                totalprice:res.data.data.total_price,
                goods_prices:res.data.data.goods_prices,
                cut_money:res.data.data.cut_money,
                deliver_cost:res.data.data.deliver_cost
              })
              that.check_message();
           }
         })
        }
      },
      check_message:function(){
        var productData=this.data.productData
        for(var p=0;p<productData.length; p++){
          if(typeof(productData[p]['form_setting'])=='undefined'){
            return false
          }
          if(productData[p]['goods_type'] == 0){
            this.data.isChooseAddress = true;
          }
          var form_setting=productData[p]['form_setting']
          for(var i=0;i<form_setting.length; i++){
            if(form_setting[i]['required']==1&&form_setting[i]['show']==1){
              if(typeof(form_setting[i]['value'])=='undefined'||form_setting[i]['value']==''){
                if(form_setting[i]['type']=='text'||form_setting[i]['type']=='textarea'||form_setting[i]['type']=='phone'||form_setting[i]['type']=='id_card'||form_setting[i]['type']=='car_number'){
                  return false
                }
                if(form_setting[i]['type']=='radio'||form_setting[i]['type']=='checkbox'){
                  return false
                }
                if(form_setting[i]['type']=='file'){
                  return false
                }
                if(form_setting[i]['type']=='date'){
                  return false
                }
                if(form_setting[i]['type']=='datetime'){
                  return false
                }
              }
              if(form_setting[i]['type']=='phone'){
                var myreg=/^1\d{10}$/
                 if (!myreg.test(form_setting[i]['value'])) {
                   return false
                 }
               }
               if(form_setting[i]['type']=='id_card'){
                var myreg = /^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
                 if (!myreg.test(form_setting[i]['value'])) {
                   return false
                 }
               }  
            }
          }

        }
        if(this.data.chooseAddress.id==0 && this.data.isChooseAddress == true){
          this.setData({
            noSubmitOrder:true
          })
        }else{
          this.setData({
            noSubmitOrder:false
          })
        }
      },
      updatemessage:function(){
          if(typeof(this.data.productData)!='undefined'){
            var productData=this.data.productData
            productData[this.data.message_index]['form_setting']=this.data.message
            this.setData({
              productData:productData
            })
            this.check_message();
          }
      },
      form_setting_index:function(e){
         this.setData({
           message_index:e.detail
         })
      },
      editmessage:function(e){
          this.triggerEvent('editmessage',e.detail)
      },
      updatecar:function(e){
        this.setData({
          productData:wx.getStorageSync('confirmorder')
        })
        this.check_message();
        this.updatedata()
      },
      deliverChange:function(e){
        var array=this.data.array
        this.setData({
            activeDeliverMethod:array[e.detail.value].id,
            index:e.detail.value,
            deliver_type:array[e.detail.value].id
        })
        if(array[e.detail.value].id==2){
          var selfAddress=[ {
            title: "选择自提点", 
            right:wx.getStorageSync('selfAddr')!=''?wx.getStorageSync('selfAddr'):'请选择自提点',
            link: {
                type: "selfaddress",
                parent: "confirm_order_selfmention",
                list: JSON.stringify(this.data.selfmention_list)
            }
        }, {
          title: "提货人姓名",
          rightType: "input",
          name: "receiverName",
          hiddenArrow: !0,
          input: {
              value:wx.getStorageSync('receiverName'),
              placeholder: "输入提货人姓名"
          }
      }, {
          title: "提货人手机号",
          rightType: "input",
          name: "receiverPhone",
          hiddenArrow: !0,
          input: {
                value:wx.getStorageSync('receiverPhone'),
                placeholder: "输入提货人手机号"
            }
        } ]
        this.setData({
          selfAddress:selfAddress
        })
        }
        //this.page_data();
      },
      makeorder:function(){
        var that=this
        that.setData({
          waitting:true
        })
        var error_show=this.data.error_show
        var message=this.data.message
        var form_info=[];
        if(message.length>0){
          form_info=[{
            name:'昵称',
            type:'text',
            options:[],
            key:'text_0',
            value:message[0].nick_name
          },{
            name:'手机号码',
            type:'text',
            options:[],
            key:'text_1',
            value:message[0].mobile
          }]
        }
         wx.request({
          url: app.globalData.siteroot,
           data: {
             do:'Message_getPushTmplIds',
             temp_name:'submit_order,pay_success'
          },
         success: function(res) {
          that.setData({
            waitting:false
          })
          if(that.data.error_show){
            wx.showToast({
              title:that.data.error_text,
              icon: 'none',
              duration: 1500
            })
          }else{
            var submit_order=res.data.info.submit_order
            var pay_success=res.data.info.pay_success
            var address_id=that.data.address_id
            if(that.data.order_type==2){
              var goods=wx.getStorageSync('confirmgoods')
            }else{
              if(typeof(that.data.newlist)=='undefined'){
                var goodslist=that.data.productData
              }else{
                var goodslist=that.data.newlist
              }
              var goods=[];
              for(var i=0;i<goodslist.length; i++){
                var count=typeof(goodslist[i]['count'])!='undefined'?goodslist[i]['count']:goodslist[i]['num']
                if(that.data.page_type=='seckill'){
                  var obj = {
                    pic:goodslist[i]['pic'],
                    goods_name:goodslist[i]['goods_name'],
                    goods_id:goodslist[i]['goodsid'],
                    sku_id:goodslist[i]['sku_id'],
                    num:count,
                    template_id:0,
                    form_info:goodslist[i]['form_setting']
                   }
                }else{
                  var obj = {
                    goods_id:goodslist[i]['id'],
                    sku_id:goodslist[i]['sku_id'],
                    num:count,
                    template_id:0,
                    form_info:goodslist[i]['form_setting']
                   }
                }
                 goods.push(obj);
             }
            }
          if(that.data.noSubmitOrder){
            wx.showToast({
              title:'请填写商品留言信息',
              icon: "none"
            });
            return false
          }
          if(submit_order==''&&pay_success==''){
            wx.request({
              url: app.globalData.siteroot,
               data: {
                 do:'Order_createOrder',
                 address_id:address_id,
                 pay_type:0,
                 coupon_id:that.data.coupon_id,
                 activity_id:that.data.activity_id,
                 launch_id:that.data.orderid,
                 deliver_type:that.data.deliver_type,
                 user_id:wx.getStorageSync('user_id'),
                 buyer_remarks:that.data.buyer_remarks, 
                 order_type:that.data.order_type,
                 order_goods_type:that.data.order_goods_type,
                 goods:goods,
              },
             success: function(res) {
                var order_no=res.data.data.order_no
                wx.setStorageSync('order_id',res.data.data.order_id);
                if(res.data.code==0){
                  wx.showToast({
                    title:res.data.msg,
                    icon: 'none',
                    duration: 1500
                  })
                }else{
                  if(that.data.deliver_type==4){
                    wx.reLaunch({
                      url: '/yb_shopv2/native/all/index?type=pay_result'
                    })
                    return false;
                  }
                  wx.request({
                    url: app.globalData.siteroot,
                     data: {
                       do:'Pay_orderPay',
                       order_no:order_no,
                       order_type:0,
                       user_id:wx.getStorageSync('user_id')
                    },
                   success: function(res) {
                     if(res.data.code==0){
                      wx.showToast({
                        title:res.data.msg,
                        icon: 'none',
                        duration: 1500
                      })
                     }else{
                      wx.requestPayment({
                        timeStamp: res.data.info.timeStamp,
                        nonceStr: res.data.info.nonceStr,
                        package: res.data.info.package,
                        signType: res.data.info.signType,
                        paySign: res.data.info.paySign,
                        success(res1) {
                           wx.showToast({
                            title: '支付成功',
                            icon: 'none',
                            duration: 1500
                          })
                          wx.request({
                            url: app.globalData.siteroot,
                             data: {
                               do:'Test_pay',
                               o:order_no,
                            },
                           success: function(res) {},
                           fail:function(){}
                          });
                          wx.reLaunch({
                            url: '/yb_shopv2/native/all/index?type=pay_result',
                          })
                        },
                        fail(res1) {
                          wx.showModal({
                            content: "支付失败",
                            showCancel: false,
                            confirmText: "确定",
                            confirmColor: "#576B95",
                            success: function (res) {
                              wx.redirectTo({
                                url: '/yb_shopv2/native/all/index?type=mine_order&tab=1&cancel=1',
                              })
                            },
                            fail: function () {
                            }
                        })
                        }
                      })
                     }
                  
                   }
                 })
                }
             }
           })
          }else{
            if(submit_order!=''&&pay_success!=''){
              wx.requestSubscribeMessage({
                tmplIds: [submit_order,pay_success],
                success (res) {
                    that.Order_createOrder(address_id,goods)
                }
                })
            }else if(submit_order==''&&pay_success!=''){
              wx.requestSubscribeMessage({
                tmplIds: [pay_success],
                success (res) {
                    that.Order_createOrder(address_id,goods)
                }
                })
            }else if(submit_order!=''&&pay_success==''){
              wx.requestSubscribeMessage({
                tmplIds: [submit_order],
                success (res) {
                    that.Order_createOrder(address_id,goods)
                }
                })
            }
          }
          }
         }
       }) 
      },
      Order_createOrder:function(address_id,goods){
        var that=this
        wx.request({
          url: app.globalData.siteroot,
           data: {
             do:'Order_createOrder',
             address_id:address_id,
             pay_type:0,
             coupon_id:that.data.coupon_id,
             activity_id:that.data.activity_id,
             launch_id:that.data.orderid,
             deliver_type:that.data.deliver_type,
             user_id:wx.getStorageSync('user_id'),
             buyer_remarks:that.data.buyer_remarks, 
             order_type:that.data.order_type,
             order_goods_type: that.data.goods_type,
             goods:goods,
          },
         success: function(res) {
            var order_no=res.data.data.order_no
            wx.setStorageSync('order_id',res.data.data.order_id);
            if(res.data.code==0){
              wx.showToast({
                title:res.data.msg,
                icon: 'none',
                duration: 1500
              })
            }else{
              if(that.data.deliver_type==4){
                wx.reLaunch({
                  url: '/yb_shopv2/native/all/index?type=pay_result'
                })
                return false;
              }
              wx.request({
                url: app.globalData.siteroot,
                 data: {
                   do:'Pay_orderPay',
                   order_no:order_no,
                   order_type:0,
                   user_id:wx.getStorageSync('user_id')
                },
               success: function(res) {
                 if(res.data.code==0){
                  wx.showToast({
                    title:res.data.msg,
                    icon: 'none',
                    duration: 1500
                  })
                 }else{
                  wx.requestPayment({
                    timeStamp: res.data.info.timeStamp,
                    nonceStr: res.data.info.nonceStr,
                    package: res.data.info.package,
                    signType: res.data.info.signType,
                    paySign: res.data.info.paySign,
                    success(res1) {
                       wx.showToast({
                        title: '支付成功',
                        icon: 'none',
                        duration: 1500
                      })
                      wx.reLaunch({
                        url: '/yb_shopv2/native/all/index?type=pay_result',
                      })
                    },
                    fail(res1) {
                      wx.showModal({
                        content: "支付失败",
                        showCancel: false,
                        confirmText: "确定",
                        confirmColor: "#576B95",
                        success: function (res) {
                        wx.redirectTo({
                            url: '/yb_shopv2/native/all/index?type=mine_order&tab=1&cancel=1',
                          }) 
                        },
                        fail: function () {
                        }
                    })
                    }
                  })
                 }
               }
             })
            }
         }
       })
      },
      remarkschange:function(e){
        if(typeof(e.detail.value)!='undefined'&&e.detail.value!='')
        {
          this.setData({
            buyer_remarks:e.detail.value
           })
        }
      },
      couponChange:function(e){
        var that=this
        var coupon_list=this.data.coupon_list
        this.setData({
          couponindex:e.detail.value,
          coupon_id:coupon_list[e.detail.value].id
         })
         if(this.data.activity_id!=''){
          console.log(this.data.activity_id)
         }else{
          wx.request({
            url: app.globalData.siteroot,
             data: {
               do:'Order_orderPrice',
               address_id:that.data.address_id,
               coupon_id:coupon_list[e.detail.value].id,
               deliver_type:that.data.deliver_type,
               pay_type:that.data.pay_type,
               user_id:wx.getStorageSync('user_id'),
               order_type:that.data.order_type,
               order_goods_type:that.data.order_goods_type,
               goods:that.data.order
            },
           success: function(res) {
             if(that.data.deliver_type==3){
              that.data.showAddress.link.longitude = Number(res.data.data.local_info.postion_lng), 
              that.data.showAddress.link.latitude = Number(res.data.data.local_info.postion_lat),
              that.data.showAddress.link.covers = JSON.stringify([ {
                  longitude: Number(res.data.data.local_info.postion_lng),
                  latitude: Number(res.data.data.local_info.postion_lat)
              } ]),
              that.data.showAddress.link.circles = JSON.stringify([ {
                  longitude: Number(res.data.data.local_info.postion_lng),
                  latitude: Number(res.data.data.local_info.postion_lat),
                  color: "#ff0000AA",
                  fillColor: "#a9d6ff88",
                  strokeWidth: 1,
                  radius: 1e3 * Number(res.data.data.local_info.radius)
              } ])
              that.setData({
                showAddress: that.data.showAddress
              })
             }
             if(res.data.data.deliver_type.length>0){
               that.setData({
                activeDeliverMethod:res.data.data.deliver_type[0].id
               })
             }
            that.setData({
              selfmention_list:res.data.data.selfmention_list,
              coupon_price:res.data.data.coupon_price,
              deliver_cost:res.data.data.deliver_cost,
              array:res.data.data.deliver_type,
              productData:res.data.data.goods_list,
              goods_prices:res.data.data.goods_prices
            })
            that.check_message();
            that.totalprice()
           }
          
          })
         }
         
      },
      totalprice(){
        var spec=this.data.productData
        var allCount=0;
        var totalprice=0;
        for (var i = 0; i <spec.length; i++) {
          allCount=parseInt(allCount)+parseInt(spec[i]['num'])
          totalprice=totalprice+spec[i]['num']*spec[i]['price'];
        }
        totalprice=totalprice+this.data.deliver_cost-this.data.coupon_price
        this.setData({
          newlist:spec,
          allCount:allCount,
          totalprice:totalprice.toFixed(2)
        })
      },
      updatedata(){
        var that=this
        var address_id=this.data.address_id
        var list=this.data.productData
        var goods_type=this.data.productData[0].goods_type
        this.setData({
          goods_type:goods_type
        })
        var order=[];
        for(var s=0;s<list.length; s++){
          if(typeof(list[s].goods_id)!='undefined'){
            var goods_id=list[s].goods_id;
          }else{
            var goods_id=list[s].id;
          }

          if(typeof(list[s].num)!='undefined'){
            var num=list[s].num;
          }else{
            var num=list[s].count;
          }

          var obj = {
            goods_id:goods_id,
            sku_id:list[s].sku_id,
            num:num,
            template_id:0
           }
           order.push(obj);
        }
        that.setData({
          order:order
        })
        var activity=0
        var activity_id=0
        if(this.data.page_type=='group2'){
          that.setData({
            activity:'group2',
            activity_id:wx.getStorageSync('confirmorder')[0].activity_id
          })
          activity='group2'
          activity_id=wx.getStorageSync('confirmorder')[0].activity_id
        }else if(this.data.page_type=='integral'){
          that.setData({
            activity:'integral',
            activity_id:wx.getStorageSync('confirmorder')[0].activity_id
          })
          activity='integral'
          activity_id=wx.getStorageSync('confirmorder')[0].activity_id
        }else if(this.data.page_type=='seckill'){
          that.setData({
            activity:'seckill',
            activity_id:wx.getStorageSync('confirmorder')[0].activity_id
          })
          activity='seckill'
          activity_id=wx.getStorageSync('confirmorder')[0].activity_id
        }else{
          if(typeof(list[0])!='undefined'){
            activity=list[0].activity
            activity_id=list[0].activity_id
            that.setData({
              activity:activity,
              activity_id:activity_id
            })
          }
        }
       
        if(activity=='integral'){
          that.setData({
            order_type:5
          })
          wx.request({
            url: app.globalData.siteroot,
             data: {
               do:'Order_orderPrice',
               address_id:address_id,
               activity_id:activity_id,
               deliver_type:that.data.deliver_type,
               pay_type:that.data.pay_type,
               user_id:wx.getStorageSync('user_id'),
               order_type:5,
               order_goods_type:that.data.order_goods_type,
               goods:order
            },
           success: function(res) {
            var productData=res.data.data.goods_list
            var allCount=0
            if(that.data.deliver_type==2){
                var selfAddress=that.data.selfAddress
                selfAddress[0].link.list=res.data.data.selfmention_list
                that.setData({
                  selfAddress:selfAddress
                })
            }
            if(that.data.deliver_type==3){
              that.data.showAddress.link.longitude = Number(res.data.data.local_info.postion_lng), 
              that.data.showAddress.link.latitude = Number(res.data.data.local_info.postion_lat),
              that.data.showAddress.link.covers = JSON.stringify([ {
                  longitude: Number(res.data.data.local_info.postion_lng),
                  latitude: Number(res.data.data.local_info.postion_lat)
              } ]),
              that.data.showAddress.link.circles = JSON.stringify([ {
                  longitude: Number(res.data.data.local_info.postion_lng),
                  latitude: Number(res.data.data.local_info.postion_lat),
                  color: "#ff0000AA",
                  fillColor: "#a9d6ff88",
                  strokeWidth: 1,
                  radius: 1e3 * Number(res.data.data.local_info.radius)
              } ])
              var showAddress=that.data.showAddress
              that.setData({
                showAddress:showAddress
              })
             }
            for (var i = 0; i <productData.length; i++) {
              productData[i]['price']=res.data.data.goods_prices.toFixed(2)
              productData[i]['hideCount']=true
              productData[i]['count']=productData[i]['num']
              allCount=parseInt(allCount)+parseInt(productData[i]['num'])
            }
            if(res.data.data.deliver_type.length>0){
              that.setData({
               activeDeliverMethod:res.data.data.deliver_type[0].id
              })
            }
              that.setData({
                selfmention_list:res.data.data.selfmention_list,
                deliver_cost:res.data.data.deliver_cost,
                allCount:allCount,
                goods_prices:res.data.data.total_price,
                totalprice:res.data.data.total_price,
                integral:res.data.data.integral,
                integral_score:res.data.data.integral,
                array:res.data.data.deliver_type,
                productData:productData
              })
              that.check_message();
              if(res.data.data.goods_list.length>0){
                if(res.data.data.coupon_list.length>0)
                {
                  var chiobj =[];
                  for(var i=0;i<res.data.data.coupon_list.length;i++)
                  {
                    chiobj.push(res.data.data.coupon_list[i])
                  }
                  chiobj.push({
                    id: 0,
                    title: "不使用优惠券"
                  })
                  that.setData({
                    coupon_list:chiobj,
                    coupon_id:res.data.data.coupon_list[0].id,
                   })
                }
                that.setData({
                  couponindex:0,
                  goods_prices:res.data.data.goods_prices
                 })
              }else{
                var data=that.data.productData
                var allCount=0;
                for (var i = 0; i <data.length; i++) {
                  allCount=parseInt(allCount)+parseInt(data[i]['count'])
                }
                that.setData({
                  error_show:true,
                  error_text:res.data.msg,
                  newlist:allCount,
                  allCount:0,
                  totalprice:0.00
                })
              }
           }
         })
        }  
        else if(activity=='group2'){
          that.setData({
            order_type:4
          })
          wx.request({
            url: app.globalData.siteroot,
             data: {
               do:'Order_orderPrice',
               address_id:address_id,
               activity_id:activity_id,
               deliver_type:that.data.deliver_type,
               pay_type:that.data.pay_type,
               user_id:wx.getStorageSync('user_id'),
               order_type:4,
               order_goods_type:that.data.order_goods_type,
               goods:order
            },
           success: function(res) {
            if(that.data.deliver_type==2){
              var selfAddress=that.data.selfAddress
              selfAddress[0].link.list=res.data.data.selfmention_list
              that.setData({
                selfAddress:selfAddress
              })
            }
            if(that.data.deliver_type==3){
              that.data.showAddress.link.longitude = Number(res.data.data.local_info.postion_lng), 
              that.data.showAddress.link.latitude = Number(res.data.data.local_info.postion_lat),
              that.data.showAddress.link.covers = JSON.stringify([ {
                  longitude: Number(res.data.data.local_info.postion_lng),
                  latitude: Number(res.data.data.local_info.postion_lat)
              } ]),
              that.data.showAddress.link.circles = JSON.stringify([ {
                  longitude: Number(res.data.data.local_info.postion_lng),
                  latitude: Number(res.data.data.local_info.postion_lat),
                  color: "#ff0000AA",
                  fillColor: "#a9d6ff88",
                  strokeWidth: 1,
                  radius: 1e3 * Number(res.data.data.local_info.radius)
              } ])
              var showAddress=that.data.showAddress
              that.setData({
                showAddress:showAddress
              })
             }
             if(typeof(res.data.data.deliver_type)!='undefined'){
              if(res.data.data.deliver_type.length>0){
                that.setData({
                 activeDeliverMethod:res.data.data.deliver_type[0].id
                })
              }
             }
              that.setData({
                  selfmention_list:res.data.data.selfmention_list,
                  deliver_cost:res.data.data.deliver_cost,
                  array:res.data.data.deliver_type,
                  goods_prices:res.data.data.goods_prices,
              })
            
               that.totalprice()
           }
         })
        }
        else{
          that.setData({
            order_type:1
          })
          wx.request({
            url: app.globalData.siteroot,
             data: {
               do:'Order_orderPrice',
               address_id:address_id,
               coupon_id:that.data.coupon_id,
               deliver_type:that.data.deliver_type,
               pay_type:that.data.pay_type,
               user_id:wx.getStorageSync('user_id'),
               order_type:that.data.order_type,
               order_goods_type:that.data.order_goods_type,
               goods:order
            },
           success: function(res) {
            if(that.data.deliver_type==2){
              var selfAddress=that.data.selfAddress
              selfAddress[0].link.list=res.data.data.selfmention_list
              that.setData({
                selfAddress:selfAddress
              })
          }
            if(that.data.deliver_type==3){
              that.data.showAddress.link.longitude = Number(res.data.data.local_info.postion_lng), 
              that.data.showAddress.link.latitude = Number(res.data.data.local_info.postion_lat),
              that.data.showAddress.link.covers = JSON.stringify([ {
                  longitude: Number(res.data.data.local_info.postion_lng),
                  latitude: Number(res.data.data.local_info.postion_lat)
              } ]),
              that.data.showAddress.link.circles = JSON.stringify([ {
                  longitude: Number(res.data.data.local_info.postion_lng),
                  latitude: Number(res.data.data.local_info.postion_lat),
                  color: "#ff0000AA",
                  fillColor: "#a9d6ff88",
                  strokeWidth: 1,
                  radius: 1e3 * Number(res.data.data.local_info.radius)
              } ])
              var showAddress=that.data.showAddress
              that.setData({
                showAddress:showAddress
              })
             }
              if(res.data.data.coupon_list.length>0)
              {
                var chiobj =[];
                for(var i=0;i<res.data.data.coupon_list.length;i++)
                {
                  chiobj.push(res.data.data.coupon_list[i])
                }
                chiobj.push({
                  id: 0,
                  title: "不使用优惠券"
                })
                var coupon_id=res.data.data.coupon_list[0].id
                that.setData({
                  selfmention_list:res.data.data.selfmention_list,
                  coupon_list:chiobj,
                  coupon_id:res.data.data.coupon_list[0].id,
                  couponindex:0,
                 })
              }
              wx.request({
                url: app.globalData.siteroot,
                 data: {
                   do:'Order_orderPrice',
                   address_id:address_id,
                   coupon_id:coupon_id,
                   deliver_type:that.data.deliver_type,
                   pay_type:that.data.pay_type,
                   user_id:wx.getStorageSync('user_id'),
                   order_type:that.data.order_type,
                   order_goods_type:that.data.order_goods_type,
                   goods:order
                },
               success: function(res) {
                if(that.data.deliver_type==2){
                  var selfAddress=that.data.selfAddress
                  selfAddress[0].link.list=res.data.data.selfmention_list
                  that.setData({
                    selfAddress:selfAddress
                  })
              }
                if(that.data.deliver_type==3){
                  that.data.showAddress.link.longitude = Number(res.data.data.local_info.postion_lng), 
                  that.data.showAddress.link.latitude = Number(res.data.data.local_info.postion_lat),
                  that.data.showAddress.link.covers = JSON.stringify([ {
                      longitude: Number(res.data.data.local_info.postion_lng),
                      latitude: Number(res.data.data.local_info.postion_lat)
                  } ]),
                  that.data.showAddress.link.circles = JSON.stringify([ {
                      longitude: Number(res.data.data.local_info.postion_lng),
                      latitude: Number(res.data.data.local_info.postion_lat),
                      color: "#ff0000AA",
                      fillColor: "#a9d6ff88",
                      strokeWidth: 1,
                      radius: 1e3 * Number(res.data.data.local_info.radius)
                  } ])
                  var showAddress=that.data.showAddress
                  that.setData({
                    showAddress:showAddress
                  })
                 }
                 if(res.data.data.deliver_type.length>0){
                  that.setData({
                   activeDeliverMethod:res.data.data.deliver_type[0].id
                  })
                }
                that.setData({
                  selfmention_list:res.data.data.selfmention_list,
                  coupon_price:res.data.data.coupon_price,
                  deliver_cost:res.data.data.deliver_cost,
                  array:res.data.data.deliver_type,
                  productData:res.data.data.goods_list,
                  goods_prices:res.data.data.goods_prices
                })
                that.check_message();
                that.totalprice()
               }
              
              })
          
             
           }
         })
        }
          
        

      }
    }
});
